﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame2
{
    class SkyBox //: Microsoft.Xna.Framework.GameComponent
    {
        Model model;
        Effect skyBoxEffect;
        FreeCamera camera;
        GraphicsDeviceManager graphics;

        public SkyBox(GraphicsDeviceManager graphics, FreeCamera camera)
        {
            this.camera = camera;
            this.graphics = graphics;
            model = LoadHelper.Models[LoadHelper.ModelEnum.skybox];
            skyBoxEffect = LoadHelper.Effects[LoadHelper.EffectEnum.skybox];
            skyBoxEffect.Parameters["SkyboxTexture"].SetValue(LoadHelper.Textures[LoadHelper.TextureEnum.skybox]);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (ModelMeshPart modelMeshPart in mesh.MeshParts)
                {
                    modelMeshPart.Effect = skyBoxEffect;
                }
            }
        }

        //public SkyBox(GraphicsDeviceManager graphics, FreeCamera camera)
        //{
        //    // TODO: Complete member initialization
        //    this.graphics = graphics;
        //    this.camera = camera;
        //}

        public void Draw()
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            // Draw the effect first, otherwise it will cover objects

            // Set the View and Projection matrix for the effect
            skyBoxEffect.Parameters["ViewMatrix"].SetValue(camera.view);
            skyBoxEffect.Parameters["ProjectionMatrix"].SetValue(camera.projection);
            // Draw the sphere model that the effect projects onto
            foreach (ModelMesh mesh in model.Meshes)
            {
                mesh.Draw();
            }

            // Undo the renderstate settings from the shader
            graphics.GraphicsDevice.DepthStencilState = DepthStencilState.None;
            graphics.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

        }
    }
}
